import java.util.ArrayList;
import java.util.List;

public class FilterChainPattern {
    public static void main(String[] args) {
        HtmlFilter htmlFilter = new HtmlFilter();
        EncodeFilter encodeFilter = new EncodeFilter();
        FilterChain filterChain = new FilterChain();
        filterChain.filters.add(htmlFilter);
        filterChain.filters.add(encodeFilter);
        filterChain.doFilter(new Request(), new Respose());
    }
}

class Request {
    void print(String txt) {
        System.out.println("request = " + txt);
    }
}

class Respose {
    void print(String txt) {
        System.out.println("response = " + txt);
    }

}

interface Filter {
    boolean doFilter(Request request, Respose respose, FilterChain filterChain);
}

class EncodeFilter implements Filter {
    public boolean doFilter(Request request, Respose respose, FilterChain filterChain) {
        System.out.println("encode");
        filterChain.doFilter(request, respose);
        return false;
    }
}

class HtmlFilter implements Filter {
    public boolean doFilter(Request request, Respose respose, FilterChain filterChain) {
        System.out.println("html");
        filterChain.doFilter(request, respose);
        return false;
    }
}

class FilterChain {
    List<Filter> filters = new ArrayList<Filter>();
    int index = 0;

    boolean doFilter(Request request, Respose respose) {
        if (index == filters.size()) {
            return true;
        }
        return filters.get(index++).doFilter(request, respose, this);
    }
}
